package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.history.HistoryDao;
import com.tbit.uqbike.client.pojo.TerPosition;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.HistoryService;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("historyService")
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private HistoryDao historyDao;
    @Autowired
    private CalibrateService calibrateService;

    @Override
    public List<TerPosition> getByMachineNOAndTime(Integer mapType, String machineNO, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineNO", machineNO);
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        List<TerPosition> history = historyDao.getByMachineNOAndTime(params);

        if (history.size() > 0) {
            calibrateService.calibrateOffSet(history, mapType);
        }

        return history;
    }

    @Override
    public Integer getMileageByMachineNOAndTime(String machineNO, String startTime, String endTime) {
        /**最大可以查询3天数据*/
        Date maxDate = DateTimeUtil.addDay(DateTimeUtil.StringToDateTime(startTime), 3);
        if (maxDate.getTime() < DateTimeUtil.StringToDateTime(endTime).getTime()) {
            endTime = DateTimeUtil.DateTimeToString(maxDate);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineNO", machineNO);
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        return historyDao.getMileageByMachineNOAndTime(params);
    }

}  